﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Fcdbas.Platform.Properties;
using Fcdbas.WinUI.Forms;

namespace Fcdbas.WinUI.Controls
{
    [Description("表示 Fcdbase 复选框控件，允许用户选择或清除关联选项。")]
    [ToolboxBitmap(typeof(System.Windows.Forms.CheckBox))]
    [ToolboxItemFilter("System.Windows.Forms")]
    public class cCheckBox : CheckEdit, IReference, IOwningDataSourceBinding, IReadOnly, IValidate, IControl
    {
        public cCheckBox()
        {
            this.CheckedChanged += new EventHandler(cCheckBox_CheckedChanged);
            this.CustomDisplayText += new DevExpress.XtraEditors.Controls.CustomDisplayTextEventHandler(cCheckBox_CustomDisplayText);
        }

        void cCheckBox_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (OwningDataSourceBinding != null && this.DataColumn != null)
            {

                Fcdbas.Core.IEntity en = OwningDataSourceBinding.Current as Fcdbas.Core.IEntity;
                if (en != null)
                {
                    if (en.Dirtys.ContainsKey(this.DataColumn.ColumnName))
                    {
                        //this.oldForeColor = this.ForeColor;
                        this.ForeColor = Fcdbas.WinUI.Common.AppEnvironment.AppOption.EditColor;
                    }
                    else
                    {
                        this.ForeColor = oldForeColor;
                    }
                }
            }
        }

        void cCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (OwningDataSourceBinding != null)
            {
                if (OwningDataSourceBinding.Updates == Platform.Enums.UpdatesSql.ReadOnly)
                    return;
                //if (!OwningDataSourceBinding.AllowEdit)
                //    return;
                if (!IsInput)
                    return;
                if (this.DataColumn != null && this.DataColumn.IsAssist)
                    return;
                if (!OwningDataSourceBinding.Session.IsNew && !OwningDataSourceBinding.Session.IsEdit && !OwningDataSourceBinding.Binding && !OwningDataSourceBinding.Session.RecordRefresh)
                {
                    Form frm = this.FindForm();
                    if (frm != null)
                        Fcdbas.Core.Windows.SalWindow.FireWindowActions(frm, Fcdbas.Platform.Const.AM_DataSourceFieldModified, 1, OwningDataSourceBinding.Identify);
                    IsEditing = true;
                }
            }
        }
        private cLabel bindLabel;
        public cLabel BindLabel
        {
            get
            {
                return this.bindLabel;
            }
            set
            {
                this.bindLabel = value;
            }
        }
        public string Label
        {
            get
            {
                if (bindLabel != null)
                    return bindLabel.Text;
                else
                    return "";
            }

        }
        private DataItem dataItem;
        /// <summary>
        /// 绑定的字段信息
        /// </summary>
        [Browsable(true)]
        // [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content)]
        public DataItem DataColumn
        {
            get { return dataItem; }
            set
            {
                if (value == null)
                    return;
                dataItem = value;
                if (OwningDataSourceBinding != null && OwningDataSourceBinding.Updates == Platform.Enums.UpdatesSql.ReadOnly)
                    this.ReadOnly = true;
                //this.Properties.MaxLength = dataItem.Length;
                //if (dataItem.DataType == typeof(int))
                //    IsNumber = true;
            }
        }

        private DataItem GetBindingItem()
        {
            return DataColumn;
        }

        private DataSourceBinding GetOwningDataSourceBinding()
        {
            if (this.DataBindings["EditValue"] == null)
            {
                if (OldBinding != null)
                    return OldBinding.DataSource as DataSourceBinding;
                else
                    return null;
            }

            return this.DataBindings["EditValue"].DataSource as DataSourceBinding;
        }

        #region IOwningDataSourceBinding 成员
        private Binding _oldBinding;
        public Binding OldBinding
        {
            get
            {
                return _oldBinding;
            }
            set
            {
                _oldBinding = value;
            }
        }
        [Browsable(false)]
        public DataSourceBinding OwningDataSourceBinding
        {
            get { return GetOwningDataSourceBinding(); }
        }

        [Browsable(false)]
        DataSourceBinding IOwningDataSourceBinding.OwningDataSourceBinding
        {
            get { return OwningDataSourceBinding; }
        }

        [Browsable(false)]
        public DataItem BindingItem
        {
            get
            {
                return GetBindingItem();
            }
        }
        protected override void OnEnter(EventArgs e)
        {
            BaseForm parentForm = this.FindForm() as BaseForm;
            if (parentForm != null)
                parentForm.FireOnEnterEvent(this);

            base.OnEnter(e);
        }
        
        #endregion

        #region IReadOnly 成员

        private Color oldForeColor = Color.Black;
        public bool ReadOnly
        {
            get
            {
                return this.Properties.ReadOnly;
            }
            set
            {
                this.Properties.ReadOnly = value;
                if (value)
                {
                    oldForeColor = this.ForeColor;
                    this.ForeColor = Fcdbas.WinUI.Common.AppEnvironment.AppOption.ReadOnlyColor;
                }
                else
                {
                    this.ForeColor = oldForeColor;
                }

            }
        }
        #endregion
        private bool IsInput = false;
        private Color oldBackColor;
        protected override void OnGotFocus(EventArgs e)
        {
            IsInput = true;
            oldBackColor = this.BackColor;
            this.BackColor = Fcdbas.WinUI.Common.AppEnvironment.AppOption.FocusColor;
            base.OnGotFocus(e);
        }
        protected override void OnLostFocus(EventArgs e)
        {
            IsInput = false;
            this.BackColor = oldBackColor;
            base.OnLostFocus(e);
        }
        #region IReference 成员
        public bool IsDelete
        {
            set
            {
                if (value)
                {
                    Font font = new Font(this.Font, FontStyle.Strikeout);
                    this.Font = font;
                    oldForeColor = this.ForeColor;
                    this.ForeColor = Fcdbas.WinUI.Common.AppEnvironment.AppOption.DeleteColor;
                }
                else
                {
                    Font font = new Font(this.Font, FontStyle.Regular);
                    this.Font = font;
                    this.ForeColor = oldForeColor;
                }
            }
        }
        private bool _isEditing;
        public bool IsEditing
        {
            get
            {
                return _isEditing;
            }
            set
            {
                _isEditing = value;
                //if (value)
                //{
                //    this.oldForeColor = this.ForeColor;
                //    this.ForeColor = Fcdbas.WinUI.Common.AppEnvironment.AppOption.EditColor;
                //}
                if (!value)
                {
                    this.ForeColor = oldForeColor;
                }
            }
        }
        public bool IsReference
        {
            get
            {
                return !string.IsNullOrEmpty(Reference);
            }
        }

        public string Reference
        {
            get
            {
                DataItem bindingItem = GetBindingItem();
                if (bindingItem != null)
                    return bindingItem.Reference;

                return null;
            }
        }
        public string Value
        {
            get
            {
                return this.Text;
            }
            set
            {
                this.Text = value;
            }
        }
        private string columnName;
        public string Column
        {
            get
            {
                DataItem bindingItem = GetBindingItem();
                if (bindingItem != null)
                    return bindingItem.ColumnName;
                else
                    return columnName;
            }
            set
            {
                DataItem bindingItem = GetBindingItem();
                if (bindingItem != null)
                    bindingItem.ColumnName = value;
                columnName = value;
            }
        }

        public Type DataType
        {
            get
            {
                DataItem bindingItem = GetBindingItem();
                if (bindingItem != null)
                    return bindingItem.DataType;

                return null;
            }
        }

        public object ReferenceValue
        {
            get
            {
                return this.Text;
            }
        }

        public void SetValue(object value)
        {
            if (value == null ||
                value is DBNull)
                return;

            this.Text = value.ToString();
        }

        #endregion

        public bool Validate()
        {
            return true;
            DataSourceBinding ownerDataSourceBinding = GetOwningDataSourceBinding();
            if (dataItem != null &&
                   dataItem.Required &&
                   string.IsNullOrEmpty(this.Text))
            {
                WinUI.Forms.BaseForm form = this.FindForm() as WinUI.Forms.BaseForm;
                if (form != null)
                    form.ShowMessage("字段[%%1]不能为空！", dataItem.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return ownerDataSourceBinding.Validate(this.DataColumn.ColumnName, this.Text);
        }
    }
}
